#region Imported Namespaces


using System;
using System.Collections.Generic;

using WtfManager.WindowsNative.User32;


#endregion


namespace WtfManager.WindowsNative
{
  public class ShellContextMenuItem
  {
    #region Fields


    private readonly MENUITEMINFO _menuItemInfo;
    private readonly string _name;
    private List<ShellContextMenuItem> _subItems;


    #endregion


    #region Constructors/Finalizers


    public ShellContextMenuItem(MENUITEMINFO menuItemInfo, List<ShellContextMenuItem> subItems)
    {
      SubItems = subItems;
      _menuItemInfo = menuItemInfo;
      _name = _menuItemInfo.dwTypeData.Replace("&", "");

      IconHandle = GetBitmapHandle();
    }


    #endregion


    #region Properties/Indexers


    public IntPtr IconHandle
    {
      get;
      private set;
    }


    public int ID
    {
      get
      {
        return _menuItemInfo.wID;
      }
    }


    public bool IsChecked
    {
      get
      {
        return (_menuItemInfo.fState & MFS.CHECKED) == MFS.CHECKED;
      }
    }


    public bool IsEnabled
    {
      get
      {
        return (_menuItemInfo.fState & MFS.ENABLED) == MFS.ENABLED;
      }
    }


    public string Name
    {
      get
      {
        return _name;
      }
    }


    public ShellContextMenuItem Parent
    {
      get;
      private set;
    }


    public List<ShellContextMenuItem> SubItems
    {
      get
      {
        return new List<ShellContextMenuItem>(_subItems);
      }
      private set
      {
        _subItems = value;

        foreach (ShellContextMenuItem subItem in _subItems)
        {
          subItem.Parent = this;
        }
      }
    }


    #endregion


    #region Methods


    private IntPtr GetBitmapHandle()
    {
      if (_menuItemInfo.hbmpItem != IntPtr.Zero)
      {
        return _menuItemInfo.hbmpItem;
      }

      if (_menuItemInfo.hbmpUnchecked != IntPtr.Zero)
      {
        return _menuItemInfo.hbmpUnchecked;
      }

      return _menuItemInfo.hbmpChecked;
    }


    public string GetPath()
    {
      if (Parent != null)
      {
        return Parent.GetPath() + "\\" + Name;
      }

      return Name;
    }


    #endregion
  }
}